//
//  MeViewController.m
//  oschina_song
//
//  Created by song on 16/1/25.
//  Copyright © 2016年 song. All rights reserved.
//

#import "MeViewController.h"
#import "RESideMenu.h"
#import "LoginViewController.h"
#import "OSCNotice.h"
#import "MBProgressHUD.h"
#import "Utils.h"
#import "Config.h"
#import "UIImageView+WebCache.h"

@interface MeViewController ()


@property(nonatomic , strong) IBOutlet UIImageView *portrait;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIButton *QRCodeBotton;

@property (weak, nonatomic) IBOutlet UIButton *creditButton;

@property(nonatomic , weak) IBOutlet UIButton *collectionButton;

@property(nonatomic ,weak)  IBOutlet UIButton *followingButton;

@property(nonatomic, weak) IBOutlet UIButton *fanButton;

@property(nonatomic ,strong) UIImageView *myQRCodeImageView;

@property(nonatomic ,assign) int64_t  myID;

@property(nonatomic ,strong) OSCUser *myProfile;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_portrait addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPortrait)]];

    [_QRCodeBotton addTarget:self action:@selector(showQRCode) forControlEvents:UIControlEventTouchUpInside];
    
    _myID = [Config getOwnID];
    [self setupSubViews];
    [self refreshHeaderView];
}


-(void)setupSubViews
{
    _creditButton.titleLabel.numberOfLines = 0 ;
    _collectionButton.titleLabel.numberOfLines = 0;
    _followingButton.titleLabel.numberOfLines = 0;
    _fanButton.titleLabel.numberOfLines =0;
}

-(void)refreshHeaderView
{
    BOOL isLogin = _myID !=0;
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingString:@"/notice.obj"];

    _myProfile = [Config myProfile];
    if(isLogin)
    {
      
        [_portrait sd_setImageWithURL:_myProfile.portraitURL  placeholderImage:[UIImage imageNamed:@"default-portrait"] options:SDWebImageContinueInBackground | SDWebImageHandleCookies completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        _portrait.userInteractionEnabled =NO;
        _nameLabel.text = _myProfile.name;
        
        [_creditButton setTitle:[NSString stringWithFormat:@"积分\n%@",@(_myProfile.score)] forState:UIControlStateNormal];
        [_collectionButton setTitle:[NSString stringWithFormat:@"收藏\n%@",@(_myProfile.favoriteCount)] forState:UIControlStateNormal];
        [_followingButton setTitle:[NSString stringWithFormat:@"关注\n%@",@(_myProfile.followersCount)] forState:UIControlStateNormal];
        [_fanButton setTitle:[NSString stringWithFormat:@"粉丝\n%@",@(_myProfile.fansCount)] forState:UIControlStateNormal];
    }
}


-(void)showQRCode
{
    MBProgressHUD *HUD =[Utils createHUD];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.color = [UIColor whiteColor];
    
    HUD.labelText= @"扫一扫上面的二维码，加我为好友";
    HUD.labelFont =[UIFont systemFontOfSize:13];
    HUD.labelColor =[UIColor grayColor];
    HUD.customView =self.myQRCodeImageView;
    
    [HUD addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideHUD:)]];
    
}

-(UIImageView *)myQRCodeImageView
{
    if(!_myQRCodeImageView)
    {
        UIImage *myQRCode =[Utils createQRCodeFromString:[NSString stringWithFormat:@"http://my.oschina.net/u/%llu",[Config getOwnID]]];
        _myQRCodeImageView =[[UIImageView alloc] initWithImage:myQRCode];
    }
    return _myQRCodeImageView;
}

-(void)hideHUD:(UIGestureRecognizer *)recognizer
{
    [(MBProgressHUD *)recognizer.view  hide:YES];
}

-(void)tapPortrait
{
    LoginViewController *loginVC =[[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"login"];
    
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [UITableViewCell new];
    
    cell.textLabel.text = @[@"消息", @"博客", @"团队"][indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@[@"me-message", @"me-blog", @"me-team"][indexPath.row]];
    
    return cell;
}





- (IBAction)menuClick:(UIBarButtonItem *)sender {
    
    [self.sideMenuViewController presentLeftMenuViewController];
}



@end
